package com.tpc.wdt.service;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tpc.wdt.WdtClient;
import com.tpc.wdt.utils.JDBCUtil;
import com.tpc.wdt.utils.TableSaveUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 拉取旺店通销售退货
 * 订单
 */
@Slf4j
@Service
@Transactional
public class TrInQuery {
    @Value("${wdt.sid}")
    private String sid;

    @Value("${wdt.appkey}")
    private String appkey;

    @Value("${wdt.appsecret}")
    private String appsecret;

    @Value("${wdt.url}")
    private String url;

    @Value("${wdt.warehouse_no}")
    private String warehouse_no;

    public void trInQuery(){
        WdtClient client = new WdtClient(sid, appkey, appsecret, url);

        Map<String, String> params = new HashMap<String, String>();
        params.put("warehouse_no", warehouse_no);
        params.put("start_time",DateUtil.format(new Date(),"yyyy-MM-dd") + " 00:00:00");
//        params.put("start_time","2023-11-28 00:00:00");
        params.put("end_time", DateUtil.date().toString());
        params.put("page_size","100");
        params.put("page_no","0");
        params.put("include_history","0");
        params.put("status","80");

        int page_size = 100;
        int page_no = 0;
        int count = 0;
        Connection connection = JDBCUtil.getConnection();
//        params.put("status","110");
        try {
            connection.setAutoCommit(false);
            log.info("获取销售退货入库单参数：{}",params);
            count = page_size;
            String response = client.execute("stockin_order_query_refund.php", params);

            JSONObject jsonResponse = JSONObject.parseObject(response);
            if(jsonResponse.getInteger("code")==0){
                JSONArray jsonArray = jsonResponse.getJSONArray("stockin_list");
                JSONArray jsonArrayMt = new JSONArray();
                JSONArray jsonArrayDt = new JSONArray();
                for(int i = 0 ;i<jsonArray.size();i++){
                    JSONObject jsonObjectMt = jsonArray.getJSONObject(i);
                    if(jsonObjectMt.getJSONArray("details_list")!=null){
                        JSONArray jsonArrayDetails = jsonObjectMt.getJSONArray("details_list");
                        for(int j=0;j<jsonArrayDetails.size();j++){
                            jsonArrayDt.add(jsonArrayDetails.getJSONObject(j));
                        }
                    }
                    jsonArrayMt.add(jsonObjectMt);
                }
                TableSaveUtils.tableSave(connection,"WDT_SALEINMT",jsonArrayMt);
                TableSaveUtils.tableSave(connection,"WDT_SALEINDT",jsonArrayDt);
                int total_count = jsonResponse.getInteger("total_count");
                while (count <= total_count) {
                    if(count == 0) {
                        continue;
                    }
                    log.info("获取销售退货入库单参数：{}",params);
                    count = count + page_size;
                    page_no = page_no + 1 ;
                    params.put("page_no",String.valueOf(page_no));
                    String twoResponse = client.execute("stockin_order_query_refund.php", params);
                    JSONObject jsonTwoResponse = JSONObject.parseObject(twoResponse);
                    if(jsonTwoResponse.getInteger("code")==0){
                        JSONArray twoJsonArray = jsonTwoResponse.getJSONArray("stockin_list");
                        JSONArray twoJsonArrayMt = new JSONArray();
                        JSONArray twoJsonArrayDt = new JSONArray();
                        for(int i = 0 ;i<twoJsonArray.size() ;i++){
                            JSONObject jsonObjectMt = twoJsonArray.getJSONObject(i);
                            if(jsonObjectMt.getJSONArray("details_list")!=null){
                                JSONArray jsonArrayDetails = jsonObjectMt.getJSONArray("details_list");
                                for(int j=0;j<jsonArrayDetails.size();j++){
                                    twoJsonArrayDt.add(jsonArrayDetails.getJSONObject(j));
                                }
                            }
                            twoJsonArrayMt.add(jsonObjectMt);
                        }
                        TableSaveUtils.tableSave(connection,"WDT_SALEINMT",twoJsonArrayMt);
                        TableSaveUtils.tableSave(connection,"WDT_SALEINDT",twoJsonArrayDt);
                    }
                    if (count > total_count) {
                        break;
                    }
                }
            }
            connection.commit();
        } catch (Exception e) {
            try {
                connection.rollback();
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
            e.printStackTrace();


            // TODO Auto-generated catch block

        } finally {
            JDBCUtil.close(connection);
        }


    }
}
